package com.sright.bcard.common.service.share.okhttp.auth;

import com.alibaba.fastjson.JSON;
import com.sright.bcard.common.service.share.okhttp.HttpAdapter;
import com.sright.bcard.common.service.share.okhttp.domain.AuthDomain;
import com.sright.bcard.common.service.share.okhttp.domain.AuthFullParam;
import com.sright.bcard.common.service.share.okhttp.result.AuthResult;
import com.sright.bcard.common.service.share.okhttp.serial.SerialUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class FileAuthHttpAdapter extends HttpAdapter {

    @Value("${AUTH_HOST}")
    private String host;

    /**
     * 权限校验
     */
    public AuthResult auth(AuthFullParam authDomain) {
        if(authDomain.getAuthDomain() == null) {
            log.info("参数为空:{}", authDomain.getParams());
        }
        AuthDomain authDomainAuthDomain = authDomain.getAuthDomain();
        if(authDomainAuthDomain.getType() == 1) {
            authDomainAuthDomain.setTag("c5aabdbc72ccd1ad");
        } else if (authDomainAuthDomain.getType() == 2) {
            authDomainAuthDomain.setTag("c5aabdbc72ccd1ae");
        } else {
            authDomainAuthDomain.setTag("c5aabdbc72ccd1be");
        }
        // build参数
        List params = new LinkedList<>();
        params.add(authDomainAuthDomain.getAppId());
        params.add(10086L);
        params.add("nickname");
        params.add(authDomainAuthDomain.getTag());
        params.add(authDomainAuthDomain.getFileName());
        params.add(authDomainAuthDomain.getFileSize());
        authDomain.setParams(params);
        log.info("FileAuthHttpAdapter.auth http请求参数 : {}", JSON.toJSONString(authDomain));
        Response response = doPost(host, JSON.toJSONString(authDomain));
        if(response.isSuccessful()) {
            ResponseBody body = response.body();
            try {
                String result = body.string();
                log.info("FileAuthHttpAdapter.auth 获取http请求调用结果 : {}", result);
                // 反序列化
                return SerialUtil.json2Object(result, AuthResult.class);
            } catch (Throwable e) {
                log.error("FileAuthHttpAdapter.auth 获取http请求返回结果异常,请求参数 : {}", JSON.toJSONString(authDomain));
            }
        }
        return null;
    }

    public static void main(String[] args) {
        FileAuthHttpAdapter fileAuthHttpAdapter = new FileAuthHttpAdapter();
        AuthFullParam authFullParam = new AuthFullParam();
        authFullParam.setJsonrpc("2.0");
        authFullParam.setMethod("/up/policy");
        AuthDomain authDomain = new AuthDomain();
        authDomain.setType(1);
        authDomain.setAppId(6404263);
        authDomain.setFileName("www.jpg");
        authDomain.setFileSize("1024");
        authDomain.setMemberId(10086L);
        authDomain.setNickname("nickname");
        authFullParam.setAuthDomain(authDomain);
        AuthResult auth = fileAuthHttpAdapter.auth(authFullParam);
        log.info(JSON.toJSONString(auth));
    }

}
